home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <stdlib.h>
-
- int Day_Of_Week();
-
- main(int argc, char *argv[])
- {
-
- int weekday;
-
- if(argc != 4)
- {
- printf("\nSyntax: WEEKDAY day month year (ie WEEKDAY 23 5 1990)\n");
- }
- else
- {
- weekday = Day_Of_Week(atoi(argv[1]), atoi(argv[2]), atoi(argv[3]));
-
- switch(weekday)
- {
- case 1 : printf("Sunday\n");
- break;
- case 2 : printf("Monday\n");
- break;
- case 3: printf("Tuesday\n");
- break;
- case 4: printf("Wednesday\n");
- break;
- case 5: printf("Thursday\n");
- break;
- case 6: printf("Friday\n");
- break;
- case 7: printf("Saturday\n");
- break;
- default: printf("Invalid date specified\n");
- printf("Day = %s, Month = %s, Year = %s\n",argv[1], argv[2], argv[3]);
- break;
- }
- }
- }
-
- /***************************************************************************/
- int Day_Of_Week(int day, int month, int year)
- /***************************************************************************/
- /*
- FUNCTION: Day_Of_Week
-
- DESCRIPTION: This function returns the day of week given the month, day
- and year.
-
- INPUT: day, month, year - integer values for each with year being
- the full 4 digit value (1990). Valid years are between 1 and
- 9999 although, given the iterations of our calendar system the
- smaller year values do not make much sense.
-
- RETURNS: day of week (1-7 = Sunday - Saturday). 0 is returned if
- specified date is invalid (ie 31-FEB-1990)
-
- */
- {
-
- #define DEC 12
- #define NOV 11
- #define OCT 10
- #define SEP 9
- #define AUG 8
- #define JUL 7
- #define JUN 6
- #define MAY 5
- #define APR 4
- #define MAR 3
- #define FEB 2
- #define JAN 1
-
- long unsigned day_count;
- int leap_year, week_day;
-
- /* Calculate number of days since 01-JAN-0001 */
-
- if((year > 1) && (year <= 9999)) /* is the year range valid? */
- {
- day_count = (year * 365);
- day_count += ((year-1)/4); /* take into account leap years */
- day_count -= ((year-1)/100); /* subtract non-leap years (every 100) */
- day_count += ((year-1)/400); /* add leap years (every 400) */
-
- /* Leap years occur every 4 years except years divisable by 100. The */
- /* exception are the years divisable by 400 which are leap years */
-
- leap_year = ((((year % 4) == 0) && ((year % 100) != 0)) || (year % 400 == 0));
-
- if((month >= 1) && (month <= 12))
- {
- switch(month)
- {
- /* add all days up to current month */
-
- case DEC : day_count += 30;
- if((month == DEC)&&(day > 31)) return(0);
- case NOV : day_count += 31;
- if((month == NOV)&&(day > 30)) return(0);
- case OCT : day_count += 30;
- if((month == OCT)&&(day > 31)) return(0);
- case SEP : day_count += 31;
- if((month == SEP)&&(day > 30)) return(0);
- case AUG : day_count += 31;
- if((month == AUG)&&(day > 31)) return(0);
- case JUL : day_count += 30;
- if((month == JUL)&&(day > 31)) return(0);
- case JUN : day_count += 31;
- if((month == JUN)&&(day > 30)) return(0);
- case MAY : day_count += 30;
- if((month == MAY)&&(day > 31)) return(0);
- case APR : day_count += 31;
- if((month == APR)&&(day > 30)) return(0);
- case MAR : day_count += (leap_year) ? 29 : 28;
- if((month == MAR)&&(day > 31)) return(0);
- case FEB : day_count += 31;
- if(((month == FEB)&&(leap_year)&&(day > 29))||
- ((month == FEB)&&(!leap_year)&&(day > 28))) return(0);
- case JAN : if((month == JAN)&&(day > 31)) return(0);
- if(day < 1) return(0);
- break;
- }
-
- day_count += day;
-
- week_day = (day_count % 7) + 1;
-
- return(week_day);
- }
- else return(0);
- }
- else return(0);
- }